home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1998 June / SGI Freeware 1998 June.iso / dist / fw_UMINNgopher.idb / usr / freeware / src / gopher_1.12 / emacs / gopher.el.z / gopher.el
Lisp/Scheme  |  1997-09-09  |  48KB  |  1,418 lines

  1. ;;; gopher.el --- an emacs gopher client
  2.  
  3. ;; Copyright (C) 1992 scott snyder
  4.  
  5. ;; Author: scott snyder <snyder@d0gsc.fnal.gov>
  6. ;; Created: 29 Jun 1992
  7. ;; Version: 1.0
  8. ;; Keywords: gopher
  9.  
  10. ;; This file is not part of GNU Emacs, but is distributed under the
  11. ;; same conditions.
  12. ;;
  13. ;; GNU Emacs is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY.  No author or distributor
  15. ;; accepts responsibility to anyone for the consequences of using it
  16. ;; or for whether it serves any particular purpose or works at all,
  17. ;; unless he says so in writing.  Refer to the GNU Emacs General Public
  18. ;; License for full details.
  19. ;;
  20. ;; Everyone is granted permission to copy, modify and redistribute
  21. ;; GNU Emacs, but only under the conditions described in the
  22. ;; GNU Emacs General Public License.   A copy of this license is
  23. ;; supposed to have been given to you along with GNU Emacs so you
  24. ;; can know your rights and responsibilities.  It should be in a
  25. ;; file named COPYING.  Among other things, the copyright notice
  26. ;; and this notice must be preserved on all copies.
  27. ;;
  28. ;;
  29. ;; An emacs gopher client.  Currently supports directory, text, CSO,
  30. ;; index, and telnet objects.
  31. ;; Requires forms.el and background.el.
  32. ;; 
  33. ;; Written by scott snyder <snyder@d0gsc.fnal.gov>
  34. ;; Some code borrowed from GNUS (by Masanobu UMEDA).
  35. ;; Some code (bookmarks, xterms, error handling) contributed
  36. ;; by Stewart Clamen <clamen@cs.cmu.edu>.
  37.  
  38. ;;; Commentary:
  39. ;; OPERATING INSTRUCTIONS
  40. ;;
  41. ;; To use, `M-x gopher'.  To specify a different root server, use
  42. ;; `C-u M-x gopher'.  If you want to use bookmarks, set the variable
  43. ;; gopher-support-bookmarks appropriately.
  44. ;;
  45. ;; In directory mode:
  46. ;;   Space or return selects the line point is on.
  47. ;;   With a numeric prefix argument, select that object.
  48. ;;   `q', `l', or `u' will return to the previous node.
  49. ;;   `n' and `p' to the next and previous lines.
  50. ;;   `a' will add an object to your bookmark list.
  51. ;;   `v' will display your bookmark list.
  52. ;;   `=' gives detailed information about an object.
  53. ;;  In the bookmark list, all of the above (except `a'), plus:
  54. ;;   `C-k' will delete an object from the bookmark list.
  55. ;;   `C-y' will yank the most recently deleted bookmark object back into
  56. ;;         the bookmark buffer.
  57. ;;   `s' will save the bookmark list.
  58. ;;
  59. ;;   All commands which operate on a specific object can take an optional
  60. ;;   numeric prefix argument giving the index of the object on which
  61. ;;   to operate.
  62. ;;
  63. ;; In document mode:
  64. ;;   Space pages forward.
  65. ;;   Delete pages backward.
  66. ;;   `q', `l' or `u' returns to the last node.
  67. ;;
  68. ;; In the CSO entry form:
  69. ;;    `C-c RET' performs a look-up, based on the field contents
  70. ;;      you've filed in.
  71. ;;    `C-c l' returns to the previous node.
  72. ;;
  73. ;; Telnets:
  74. ;;    If you have an X server set, gopher will try to create an xterm
  75. ;;    running telnet.  If not, the emacs-lisp telnet mode will be used.
  76. ;;    From the emacs-lisp telnet mode, use `C-c l' to kill the session
  77. ;;    and return to the previous node.
  78. ;;    See also the variable gopher-telnet-command.
  79. ;;
  80. ;; Note:
  81. ;;   If gopher consistently hangs while trying to retrieve an object,
  82. ;;   try turning on gopher-buggy-accept (which see).
  83. ;;
  84. ;; VMS notes:
  85. ;;   To use this on VMS, you'll need my emacs subprocess patches (recently
  86. ;;   posted on gnu.emacs.sources; if you can't find them, send me mail).
  87. ;;   To be able to run telnet in a separate decterm, you'll also need to
  88. ;;   to (setq shell-file-name "docmd") and create the file
  89. ;;   emacs_library:[etc]docmd.com containing the following:
  90. ;;     $ if p1 .eqs. "-C" then p1 = ""
  91. ;;     $ deass sys$input
  92. ;;     $ 'p1 'p2 'p3 'p4 'p5 'p6 'p7 'p8
  93. ;;     $ eoj
  94.  
  95. ;;; Change Log:
  96. ;;
  97. ;; Version 1.00    29-AUG-1992
  98. ;;  * Added gopher-buggy-accept.
  99. ;;  * Reworked telnet stuff to use an arbitrary command string to start up
  100. ;;    the telnet process.  This can be used to start the telnet in a
  101. ;;    separate terminal window.
  102. ;;    Based on code from Stewart Clamen.
  103. ;;  * Stewart Clamen <clamen@cs.cmu.edu> added bookmarks.
  104. ;;  * Added 's key binding to save bookmarks.
  105. ;;  * Added a prefix argument to gopher-directory-buffer and
  106. ;;    gopher-add-bookmark.
  107. ;;  * Added standard emacs-lisp header.
  108. ;;  * Stewart Clamen <clamen@cs.cmu.edu> added some error trapping and
  109. ;;    recovery (gopher-retrieve-document-cleanly).
  110. ;;  * Appended node description to the node's buffer's name.
  111. ;;  * Reformat bookmark buffers when returning to them via gopher-last-node
  112. ;;    or when an item is deleted.
  113. ;;  * Added gopher-yank-bookmark.
  114. ;;  * Added gopher-bookmark-modified-tick to prevent reformatting bookmark
  115. ;;    buffers needlessly.
  116. ;;
  117. ;; Version 0.92    27-JUL-1992
  118. ;;  * Added gopher-hostname-aliases.
  119. ;;
  120. ;; Version 0.91    30-JUN-1992
  121. ;;  * Deal with servers which send stuff after the CR.
  122. ;;  * Prevent gopher-directory-show-object from clearing the read-only flag.
  123. ;;  * Allow specification of port number in `C-u M-x gopher'.
  124. ;;
  125. ;; Version 0.9     29-JUN-1992
  126. ;;  * Initial release.
  127.  
  128. ;;; Code:
  129.  
  130. (require 'electric)
  131. (require 'forms)
  132.  
  133. ;; background has the same name as an epoch function. 
  134. ;; Rename it to gopher-background...
  135. ;; also, the version i got from the archive didn't have a provide...
  136. (if (not (fboundp 'gopher-background))
  137.     (if (fboundp 'background)
  138.         (let ((old-background (symbol-function 'background)))
  139.           (load-library "background")
  140.           (fset 'gopher-background (symbol-function 'background))
  141.           (fset 'background old-background))
  142.       (load-library "background")
  143.       (fset 'gopher-background (symbol-function 'background))
  144.       ))
  145.  
  146. (defvar gopher-root-node (vector ?1 "root" "" "gopher.micro.umn.edu" 70)
  147.   "The root gopher server, as a gopher object.")
  148.  
  149. (defvar gopher-directory-mode-hook nil
  150.   "*Invoked when entering a new gopher directory.")
  151. (defvar gopher-directory-mode-map (make-keymap)
  152.   "Keymap for gopher-directory-mode.")
  153.  
  154. (defvar gopher-document-mode-hook nil
  155.   "*Invoked when showing gopher document.")
  156. (defvar gopher-document-mode-map (make-keymap)
  157.   "Keymap for gopher-document-mode.")
  158.  
  159. (defvar gopher-form-mode-hooks nil
  160.   "*Invoked with entering a gopher form (i.e., for CSO).")
  161. (defvar gopher-form-mode-map (make-keymap)
  162.   "Keymap for gopher-form-mode.")
  163.  
  164. (defvar gopher-tmp-buf nil
  165.   "Buffer used to receive output from gopher.")
  166.  
  167. (defvar gopher-debug-read t
  168.   "*If non-nil, show the current status about reading the gopher server output.")
  169.  
  170. ;; On some systems (such as SGI Iris), accept-process-output doesn't seem
  171. ;; to return for the last packet received on a connection.  Turn this on
  172. ;; to work around the problem, but does anyone know what causes this?
  173. (defvar gopher-buggy-accept nil
  174.   "*If non-nil, use sit-for instead of accept-process-output.
  175. If gopher consistently hangs while fetching an object, try turning this on.")
  176.  
  177. (defvar gopher-hostname-aliases
  178.   '(("128.230.33.31" . "oliver.syr.edu"))
  179.   "Emacs can't deal with raw IP addresses used as a hostname.
  180. Use this to work around...")
  181.  
  182.  
  183. (defvar gopher-support-bookmarks nil
  184.   "*If nil, do not implement bookmarks. 
  185. If 'unix or t, read and write bookmarks to ~/.gopherrc. 
  186. If a filename, read and save vector from there directly (not implemented yet).
  187. If a vector, treat as a built-in directory.")
  188.  
  189. (defconst gopher-bookmarks nil "Internal bookmark directory.")
  190. (defconst gopher-bookmarks-modified nil "Do bookmarks need to be saved?")
  191. (defconst gopher-killed-bookmark nil "The last bookmark object to be killed")
  192. (defconst gopher-bookmark-directory-p nil
  193.   "Is this buffer a bookmark directory?  A buffer-local variable.")
  194.  
  195. (defvar gopher-bookmark-modified-tick 0
  196.   "Counts each time the bookmark vector is modified.")
  197.  
  198.  
  199. (defvar gopher-telnet-command
  200.   (cond ((eq system-type 'vax-vms)
  201.          (if (getenv "DECW$DISPLAY")
  202.              "create/terminal/wait/window=(title=\"telnet\") telnet"))
  203.         (t
  204.          (if (getenv "DISPLAY")
  205.              "xterm -e telnet"))
  206.         )
  207.   "*Command to use to start a telnet session.
  208. If this is nil, the emacs-lisp telnet package will be used.
  209. The default setting is to create a terminal window running telnet
  210. if you've specified an X server, and to use the emacs-lisp telnet otherwise.")
  211.  
  212.  
  213. (defvar gopher-object-type-alist
  214.   '(( ?0   ""          gopher-document-object)
  215.     ( ?1   "/"         gopher-directory-object)
  216.     ( ?2   " <CSO>"    gopher-cso-object)
  217.     ( ?3   " <error>"  gopher-unimplemented-object)
  218.     ( ?4   " <binhex>" gopher-binary-object)
  219.     ( ?5   " <DOS>"    gopher-binary-object)
  220.     ( ?6   " <UU>"     gopher-binary-object)
  221.     ( ?7   " <?>"      gopher-index-object)
  222.     ( ?8   " <TEL>"    gopher-telnet-object)
  223.     ( ?9   " <bin>"    gopher-binary-object)
  224.     ( ?T   " <T>"      gopher-unimplemented-object)
  225.     ( ?s   " <)"       gopher-binary-object)
  226.     ( ?M   " <MIME>"   gopher-unimplemented-object)
  227.     ( ?h   " <html>"   gopher-unimplemented-object)
  228.     ( ?I   " <image>"  gopher-unimplemented-object)
  229.     ( ?c   " <cal>"    gopher-unimplemented-object)
  230.     ( ?g   " <GIF>"    gopher-binary-object)
  231.     )
  232.   "*Alist describing the types of gopher objects this client know about.
  233. The keys are the gopher type characters.
  234. The second element in each list is the string to tag onto the end
  235. of an object's description, to identify it to the user.
  236. The third element is the function to use to retrieve the object.
  237. It is called with two arguments: the gopher object to retrieve and
  238. the buffer which should be returned to when the user is done
  239. with this object.")
  240.  
  241.  
  242. ;;;
  243. ;;; The data structure describing a gopher object is a vector of five elements:
  244. ;;;    [ TYPE DESCR SELECTOR HOST PORT ]
  245. ;;;
  246. ;;;  TYPE is the type character.
  247. ;;;  DESCR is the human-readable description of the object.
  248. ;;;  SELECTOR is the opaque selector to be sent to HOST to retrieve the obj.
  249. ;;;  HOST is the name of the Internet host on which the object resides.
  250. ;;;  PORT is the TCP/IP port on which the host is listening.
  251. ;;;
  252. ;;;  The following macros set and fetch elements of this structure.
  253. ;;;  
  254.  
  255. (defmacro gopher-object-type (object)
  256.   "Return the gopher type of OBJECT."
  257.   (` (aref (, object) 0)))
  258.  
  259. (defmacro gopher-object-descr (object)
  260.   "Return the gopher description of OBJECT."
  261.   (` (aref (, object) 1)))
  262.  
  263. (defmacro gopher-object-selector (object)
  264.   "Return the gopher selector string for OBJECT."
  265.   (` (aref (, object) 2)))
  266.  
  267. (defmacro gopher-object-host (object)
  268.   "Return the gopher hostname for OBJECT."
  269.   (` (aref (, object) 3)))
  270.  
  271. (defmacro gopher-object-port (object)
  272.   "Return the gopher TCP port number for OBJECT."
  273.   (` (aref (, object) 4)))
  274.  
  275.  
  276. (defmacro gopher-set-object-type (object type)
  277.   "Set the gopher type of OBJECT to TYPE."
  278.   (` (aset (, object) 0 (, type))))
  279.  
  280. (defmacro gopher-set-object-descr (object descr)
  281.   "Set the gopher description of OBJECT to DESCR."
  282.   (` (aset (, object) 1 (, descr))))
  283.  
  284. (defmacro gopher-set-object-selector (object selector)
  285.   "Set the gopher selector string for OBJECT to SELECTOR."
  286.   (` (aset (, object) 2 (, selector))))
  287.  
  288. (defmacro gopher-set-object-host (object host)
  289.   "Set the gopher hostname for OBJECT to HOST."
  290.   (` (aset (, object) 3 (, host))))
  291.  
  292. (defmacro gopher-set-object-port (object port)
  293.   "Set the gopher TCP port number for OBJECT to PORT."
  294.   (` (aset (, object) 4 (, port))))
  295.  
  296.  
  297. (defmacro gopher-retrieve-document-cleanly (args handle &rest body)
  298.   "Call gopher-retrieve-document with condition-case wrapped around, 
  299. applying HANDLE if appropriate."
  300.   (` (condition-case nil
  301.          (progn
  302.            (gopher-retrieve-document (,@ args))
  303.            (,@ body))
  304.        (error (, handle)))))
  305.  
  306.  
  307. ;;
  308. ;; buffer-local variables.
  309. ;; declared here to prevent warnings from the new byte-compiler.
  310. ;;
  311.  
  312. (defvar gopher-dir nil)
  313. (defvar gopher-last nil)
  314. (defvar gopher-obj nil)
  315. (defvar gopher-telnet-process-name nil)
  316. (defvar gopher-bookmark-buffer-tick nil)
  317. (defvar forms-accept-action nil)        
  318.  
  319.  
  320. ;;;;--------------------------------------------------------------------------
  321. ;;;; main dispatching logic.
  322. ;;;;
  323.  
  324. (defun gopher (&optional askserv)
  325.   "Start a gopher session.  With C-u, prompt for a gopher server."
  326.   (interactive "P")
  327.   (if askserv
  328.       (progn
  329.         (gopher-set-object-host
  330.          gopher-root-node
  331.          (read-string "Gopher server: "
  332.                       (gopher-object-host gopher-root-node)))
  333.  
  334.         (let (portstr port)
  335.           (while (not (numberp port))
  336.             (setq portstr
  337.                   (read-string "Port: "
  338.                                (int-to-string
  339.                                 (gopher-object-port gopher-root-node))))
  340.  
  341.             (setq port (condition-case nil
  342.                            (car (read-from-string portstr))
  343.                          (error nil)))
  344.  
  345.             (if (not (numberp port))
  346.                 (progn
  347.                   (ding)
  348.                   (message "Port must be numeric")
  349.                   (sit-for 1)))
  350.             )
  351.  
  352.           (gopher-set-object-port gopher-root-node port))))
  353.  
  354.   (cond ((null gopher-support-bookmarks))
  355.         ((or (equal gopher-support-bookmarks 'unix)
  356.              (equal gopher-support-bookmarks t))
  357.          (setq gopher-bookmarks 
  358.                (gopher-read-unix-bookmarks)))
  359.         ((stringp gopher-support-bookmarks)
  360.          (gopher-read-lisp-bookmarks gopher-support-bookmarks))
  361.         ((vectorp gopher-support-bookmarks)
  362.          (setq gopher-bookmarks gopher-support-bookmarks))
  363.         (t
  364.          (message "Illformed gopher-bookmarks, assuming none")))
  365.  
  366.   (gopher-dispatch-object gopher-root-node nil))
  367.   
  368.  
  369.  
  370. (defun gopher-dispatch-object (obj lastbuf)
  371.   "Dispatch a gopher object depending on its type."
  372.   (let ((typedesc (assq (gopher-object-type obj) gopher-object-type-alist)))
  373.  
  374.     (if typedesc
  375.         (funcall (nth 2 typedesc) obj lastbuf)
  376.       (gopher-unimplemented-object obj lastbuf))))
  377.  
  378.  
  379. (defun gopher-unimplemented-object (obj lastbuf)
  380.   (error "unimplemented object type"))
  381.  
  382.  
  383. ;;;;--------------------------------------------------------------------------
  384. ;;;; utilities
  385. ;;;;
  386.  
  387. (defun gopher-next-field nil
  388.   "Returns as a string all chars between point and the next tab or newline.
  389. Point is advanced to after the tab (or to the end-of-line)."
  390.  
  391.   (let ((beg (point)) s)
  392.     (skip-chars-forward "^\t\n")
  393.     (setq s (buffer-substring beg (point)))
  394.     (if (eq (following-char) ?\t)
  395.         (forward-char))
  396.     s))
  397.  
  398.  
  399. ;; from GNUS
  400. (defun gopher-make-local-vars (&rest pairs)
  401.   ;; Take VARIABLE-VALUE pairs and makes local variables initialized to the
  402.   ;; value.
  403.   (while pairs
  404.     (make-local-variable (car pairs))
  405.     (set (car pairs) (car (cdr pairs)))
  406.     (setq pairs (cdr (cdr pairs)))))
  407.  
  408.  
  409. (defun gopher-get-tmp-buf nil
  410.   "Get a temporary buffer in which to receive gopher output."
  411.   (or (bufferp gopher-tmp-buf)
  412.       (progn
  413.         (setq gopher-tmp-buf (get-buffer-create " *gopher-tmp*"))
  414.         (buffer-flush-undo gopher-tmp-buf)))
  415.   gopher-tmp-buf)
  416.  
  417.  
  418. (defun gopher-get-dir-buf (descr)
  419.   "Get a new buffer suitable for a gopher directory or document."
  420.   (let ((buf (generate-new-buffer (concat "*gopher*" descr))))
  421.     (buffer-flush-undo buf)
  422.     buf))
  423.  
  424. (fset 'gopher-get-doc-buf (symbol-function 'gopher-get-dir-buf))
  425.  
  426.  
  427. (defun gopher-trim-blanks (str)
  428.   "Remove leading and trailing blanks from STR."
  429.   (string-match "\\`[ \t\n]*" str)
  430.   (substring str
  431.              (match-end 0)
  432.              (string-match "[ \t\n]*\\'" str (match-end 0))))
  433.  
  434.  
  435. ;;;;--------------------------------------------------------------------------
  436. ;;;; directory handling
  437. ;;;;
  438.  
  439.  
  440. (defun gopher-directory-object (obj oldbuf)
  441.   "Retrieve and display a gopher directory."
  442.  
  443.   (let ((tmpbuf (gopher-get-tmp-buf))
  444.         (dirbuf (gopher-get-dir-buf (gopher-object-descr obj))))
  445.  
  446.     ;; Get the directory...
  447.     (gopher-retrieve-document-cleanly (tmpbuf
  448.                                        (gopher-object-selector obj)
  449.                                        (gopher-object-host     obj)
  450.                                        (gopher-object-port     obj))
  451.         
  452.         (progn
  453.           (kill-buffer dirbuf)
  454.           (error "Problems retrieving directory."))
  455.  
  456.       ;; Parse it and store our internal representation in gopher-dir.
  457.       (switch-to-buffer dirbuf)
  458.       (gopher-make-local-vars
  459.        'gopher-dir (gopher-parse-directory tmpbuf)
  460.        'gopher-last oldbuf)
  461.  
  462.       ;; Format it for your viewing pleasure.
  463.       (gopher-format-directory gopher-dir dirbuf)
  464.       (goto-char (point-min))
  465.       (if (> (- (point-max) (point)) 7) (forward-char 7))
  466.  
  467.       ;; Turn on directory mode and put the description in the mode line.
  468.       (gopher-directory-mode)
  469.       (setq mode-line-buffer-identification (concat "Gopher: "
  470.                                                     (gopher-object-descr obj)))
  471.       )))
  472.  
  473.  
  474. (defun gopher-parse-directory (buf)
  475.   "Parse the gopher directory in buffer BUF into our internal representation.
  476. Returns a vector of gopher objects."
  477.  
  478.   (save-excursion
  479.     (set-buffer buf)
  480.     (goto-char (point-min))
  481.  
  482.     (let* ((len (count-lines (point-min) (point-max)))
  483.            (dir (make-vector len nil))
  484.            (i 0))
  485.  
  486.       (while (not (eobp))
  487.         (aset dir i (gopher-parse-directory-line))
  488.         (setq i (1+ i))
  489.         (forward-line 1))
  490.  
  491.       dir)))
  492.  
  493.  
  494. (defun gopher-parse-directory-line nil
  495.   "Parse the line containing point as a gopher directory entry.
  496. Returns the corresponding gopher object."
  497.  
  498.   (let (type descr selector host port)
  499.     (beginning-of-line)
  500.     (setq type (following-char))
  501.     (forward-char)
  502.     (setq descr (gopher-next-field))
  503.     (setq selector (gopher-next-field))
  504.     (setq host (gopher-next-field))
  505.     (setq port (gopher-next-field))
  506.  
  507.     (if (string-match "^[0-9]+$" port)
  508.         (setq port (string-to-int port)))
  509.  
  510.     (vector type descr selector host port)))
  511.  
  512.  
  513. (defun gopher-format-directory (dir buf)
  514.   "Print the directory vector DIR into buffer BUF."
  515.  
  516.   (save-excursion
  517.     (set-buffer buf)
  518.     (erase-buffer)
  519.     (let ((i 0)
  520.           (len (length dir)))
  521.       (while (< i len)
  522.         (gopher-format-directory-line (aref dir i) (1+ i))
  523.         (setq i (1+ i)))
  524.  
  525.       )))
  526.  
  527.  
  528. (defun gopher-format-directory-line (obj ndx)
  529.   "Insert a line describing the gopher object OBJ into the current buffer.
  530. NDX is a numeric index to display to the left of the object description."
  531.  
  532.   (let ((ndx-str (int-to-string ndx))
  533.         (typedesc (assq (gopher-object-type obj) gopher-object-type-alist)))
  534.  
  535.     ;; display the index number.  use 5 digits, right-justified.
  536.     (if (< (length ndx-str) 5)
  537.         (insert (make-string (- 5 (length ndx-str)) ? )))
  538.     (insert ndx-str)
  539.     (insert ". ")
  540.  
  541.     ;; add the object description.
  542.     (insert (gopher-object-descr obj))
  543.  
  544.     ;; add a tag indicating the gopher object type.
  545.     (insert (if typedesc
  546.                 (nth 1 typedesc)
  547.               (concat " ???" (char-to-string (gopher-object-type obj)))))
  548.  
  549.     (insert "\n")))
  550.  
  551.  
  552. (defun gopher-directory-mode nil
  553.   "Gopher directory mode.
  554.  
  555. \\{gopher-directory-mode-map}
  556. "
  557.   (use-local-map gopher-directory-mode-map)
  558.   (setq major-mode 'gopher-directory-mode)
  559.   (setq mode-name "gopher dir")
  560.   (run-hooks 'gopher-directory-mode-hook)
  561.   (setq buffer-read-only t))
  562.  
  563.  
  564. ;;; keymap for directory mode
  565. (suppress-keymap gopher-directory-mode-map)
  566. (define-key gopher-directory-mode-map "\r" 'gopher-directory-choose)
  567. (define-key gopher-directory-mode-map " " 'gopher-directory-choose)
  568. (define-key gopher-directory-mode-map "l"  'gopher-last-node)
  569. (define-key gopher-directory-mode-map "q"  'gopher-last-node)
  570. (define-key gopher-directory-mode-map "u"  'gopher-last-node)
  571. (define-key gopher-directory-mode-map "="  'gopher-directory-show-object)
  572.  
  573. ; Virginia Peck <vapeck@cs>  Mon Aug 10 1992
  574. (define-key gopher-directory-mode-map "n"  'next-line)
  575. (define-key gopher-directory-mode-map "p"  'previous-line)
  576. ;;(define-key gopher-directory-mode-map "\C-xk"  'gopher-last-node)
  577.  
  578. ; Stewart Clamen <clamen@cs.cmu.edu>  Mon Aug 17 1992
  579. (define-key gopher-directory-mode-map "v"  'gopher-display-bookmarks)
  580. (define-key gopher-directory-mode-map "a"  'gopher-add-bookmark)
  581. (define-key gopher-directory-mode-map "\C-k"  'gopher-delete-bookmark)
  582. (define-key gopher-directory-mode-map "s" 'gopher-directory-save-bookmarks)
  583. (define-key gopher-directory-mode-map "\C-y" 'gopher-yank-bookmark)
  584.  
  585.  
  586. (defun gopher-directory-nth-obj (n)
  587.   "Returns the Nth object (starting at 1) in a gopher directory buffer."
  588.   (if (or (<= n 0) (> n (length gopher-dir)))
  589.       (error "Out of range."))
  590.   (aref gopher-dir (1- n)))
  591.  
  592.  
  593. (defun gopher-directory-n (arg)
  594.   "Return the index of the object specified by ARG (starting at 1).
  595. If ARG is nil, this is the index of the current line.
  596. Otherwise, it is the value of ARG (as a prefix argument)."
  597.   (if arg
  598.       (prefix-numeric-value arg)
  599.     (if (eq (point) (point-max))
  600.     (1+ (count-lines (point-min) (point-max)))
  601.       (count-lines (point-min) (1+ (point))))))
  602.  
  603.  
  604. (defun gopher-directory-obj (arg)
  605.   "Return the gopher object given by prefix arg ARG.
  606. If it is nil, return the object given by the line point is on.
  607. Otherwise, ARG is the index of the object."
  608.   (gopher-directory-nth-obj (gopher-directory-n arg)))
  609.  
  610.  
  611. (defun gopher-directory-choose (arg)
  612.   "Choose an item from the directory, and do whatever is appropriate
  613. based on the object's type.  Default is to choose the object given by the
  614. line the cursor is on.  With numeric prefix argument N, choose object N."
  615.   (interactive "P")
  616.   (gopher-dispatch-object (gopher-directory-obj arg) (current-buffer)))
  617.  
  618.  
  619. (defun gopher-directory-show-object (arg)
  620.   "Dump the internal information in a gopher object.
  621. With numeric prefix argument N, show information about the Nth object."
  622.   (interactive "P")
  623.   (let* ((obj (gopher-directory-obj arg))
  624.          (type (gopher-object-type obj))
  625.          (typespec (assq type gopher-object-type-alist))
  626.          (typetag (if typespec (nth 1 typespec) "?"))
  627.          (typeproc (if typespec (nth 2 typespec) "?")))
  628.     (with-output-to-temp-buffer "*Gopher object*"
  629.       (princ (format "Type        : %c   `%s'   %s\n" type typetag typeproc))
  630.       (princ (format "Description : %s\n" (gopher-object-descr    obj)))
  631.       (princ (format "Selector    : %s\n" (gopher-object-selector obj)))
  632.       (princ (format "Host        : %s\n" (gopher-object-host     obj)))
  633.       (princ (format "Port        : %d\n" (gopher-object-port     obj)))
  634.       (current-buffer)
  635.       ))
  636.   (shrink-window-if-larger-than-buffer (get-buffer-window "*Gopher object*"))
  637.  
  638.   ;; shrink-window-if-larger-than-buffer screws these up...
  639.   (set-buffer-modified-p nil)
  640.   (setq buffer-read-only t))
  641.  
  642.  
  643. (defun gopher-last-node nil
  644.   "Return to the previous gopher node.
  645. By convention, a gopher buffer has the local variable gopher-last which
  646. contains the buffer to which we should return."
  647.   (interactive)
  648.   (let ((oldbuf (current-buffer)))
  649.     (if gopher-last
  650.         (progn
  651.           (switch-to-buffer gopher-last)
  652.           (kill-buffer oldbuf)
  653.       (and (gopher-bookmark-directory-p)
  654.            (> gopher-bookmark-modified-tick gopher-bookmark-buffer-tick)
  655.            (let ((ppos (1- (gopher-directory-n nil))))
  656.          (gopher-format-bookmarks)
  657.          (forward-line ppos)
  658.          (if (> (- (point-max) (point)) 7) (forward-char 7)))))
  659.       (if (and gopher-support-bookmarks
  660.                gopher-bookmarks-modified
  661.                (y-or-n-p
  662.                 "Changes have been made to the Bookmark directory.  Save? "))
  663.           (gopher-save-bookmarks))
  664.       (kill-buffer oldbuf))))
  665.  
  666.  
  667. (defun gopher-directory-save-bookmarks ()
  668.   "Save the bookmark list."
  669.   (interactive)
  670.  
  671.   (if (not (gopher-bookmark-directory-p))
  672.       (error "This isn't the bookmark directory."))
  673.  
  674.   (gopher-save-bookmarks))
  675.  
  676.  
  677. ;;;;--------------------------------------------------------------------------
  678. ;;;; bookmarks (Implemented by clamen@cs.cmu.edu)
  679. ;;;;
  680.  
  681. (defun gopher-read-unix-bookmarks ()
  682.   "Read bookmarks out of ~/.gopherrc file."
  683.   (let ((rcfile "~/.gopherrc"))
  684.     (if (file-exists-p rcfile)
  685.         (let* ((rcbuf (find-file-noselect rcfile))
  686.                (bkmks (gopher-parse-bookmark-buffer rcbuf)))
  687.           (kill-buffer rcbuf)
  688.           (setq gopher-bookmarks-modified nil)
  689.       (setq gopher-bookmark-modified-tick
  690.         (1+ gopher-bookmark-modified-tick))
  691.           bkmks)
  692.       (message "No %s exists." rcfile)
  693.       nil)))
  694.  
  695. (defun gopher-parse-bookmark-buffer (buf)
  696.   "Read buffer containing bookmarks, formatted like ~.gopherrc 
  697. in UNIX gopher client."
  698.   (save-excursion
  699.     (set-buffer buf)
  700.     (goto-char (point-min))
  701.     (if (re-search-forward "^bookmarks:\n" (point-max) t)
  702.         (let (bkmk bkmks)
  703.           (while (setq bkmk (gopher-parse-bookmark))
  704.             (setq bkmks (cons bkmk bkmks)))
  705.           (apply 'vector (reverse bkmks))))))
  706.  
  707. (defun gopher-parse-bookmark ()
  708.   "Read next bookmark.  Return a directory object."
  709.   (if (re-search-forward
  710.        "^#$\nType=\\(.+\\)\nName=\\(.+\\)\nPath=\\(.+\\)\nHost=\\(.+\\)\nPort=\\([0-9]+\\)\n" 
  711.        (point-max) t)
  712.       (vector (string-to-char
  713.                (buffer-substring (match-beginning 1) (match-end 1)))
  714.               (buffer-substring (match-beginning 2) (match-end 2))
  715.               (buffer-substring (match-beginning 3) (match-end 3))
  716.               (buffer-substring (match-beginning 4) (match-end 4))
  717.               (string-to-int
  718.                (buffer-substring (match-beginning 5) (match-end 5))))))
  719.  
  720. (defun gopher-format-bookmarks ()
  721.   "Make the current buffer (which is assumed to be a bookmark buffer)
  722. contain an up-to-date listing of the bookmark list."
  723.  
  724.   (let ((buffer-read-only nil))
  725.     (erase-buffer)
  726.     (setq gopher-dir gopher-bookmarks)
  727.  
  728.     ;; Format it for your viewing pleasure.
  729.     (gopher-format-directory gopher-dir (current-buffer))
  730.     (goto-char (point-min))
  731.     (if (> (- (point-max) (point)) 7) (forward-char 7))
  732.     (setq gopher-bookmark-buffer-tick gopher-bookmark-modified-tick)))
  733.  
  734. (defun gopher-display-bookmarks ()
  735.   "Retrieve and display the gopher bookmark directory."
  736.   (interactive)
  737.  
  738.   (if (> (length gopher-bookmarks) 0)
  739.       (let ((oldbuf (current-buffer))
  740.             (dirbuf (gopher-get-dir-buf "*Gopher Bookmarks*")))
  741.  
  742.         ;; Store our internal representation in gopher-dir.
  743.         (switch-to-buffer dirbuf)
  744.         (gopher-make-local-vars
  745.          'gopher-dir gopher-bookmarks
  746.      'gopher-bookmark-directory-p t
  747.      'gopher-bookmark-buffer-tick gopher-bookmark-modified-tick
  748.          'gopher-last oldbuf)
  749.  
  750.     (gopher-format-bookmarks)
  751.  
  752.         ;; Turn on directory mode and put the description in the mode line.
  753.         (gopher-directory-mode)
  754.         (setq mode-line-buffer-identification (concat "Gopher: *Bookmarks*"))
  755.         )
  756.     (error "No bookmarks supported.")))
  757.  
  758.  
  759. (defun gopher-save-bookmarks ()
  760.   "Save bookmarks."
  761.   (cond 
  762.    ((or (equal gopher-support-bookmarks 'unix)
  763.         (equal gopher-support-bookmarks t))
  764.     (gopher-save-unix-bookmarks))
  765.    ((stringp gopher-support-bookmarks)
  766.     (gopher-save-lisp-bookmarks gopher-support-bookmarks))
  767.    (t
  768.     (message "Illformed gopher-support-bookmarks, assuming none")))
  769.  
  770.   (setq gopher-bookmarks-modified nil))
  771.  
  772.  
  773. (defun gopher-save-unix-bookmarks ()
  774.   "Save bookmarks out to ~/.gopherrc file."
  775.   (save-excursion
  776.     (let* ((rcfile "~/.gopherrc")
  777.            (new-file-p (not (file-exists-p rcfile)))
  778.            (rcbuf (find-file-noselect rcfile)))
  779.       (set-buffer rcbuf)
  780.       (if new-file-p
  781.           (insert "bookmarks:\n")
  782.         (goto-char (point-min))
  783.         (if (re-search-forward "^bookmarks:\n" nil t)
  784.             (delete-region (point) (point-max))
  785.           (goto-char (point-max))
  786.           (insert "bookmarks:\n")))
  787.  
  788.       ;; Now, insert defined bookmarks into file
  789.   
  790.       (let ((obj-count 0))
  791.         (while (< obj-count (length gopher-bookmarks))
  792.           (let ((obj (aref gopher-bookmarks obj-count)))
  793.             (insert "#"
  794.                     "\nType=" (gopher-object-type obj)
  795.                     "\nName=" (gopher-object-descr obj)
  796.                     "\nPath=" (gopher-object-selector obj)
  797.                     "\nHost=" (gopher-object-host obj)
  798.                     "\nPort=" (int-to-string (gopher-object-port obj))
  799.                     "\n")
  800.             (setq obj-count (1+ obj-count)))))
  801.  
  802.       (write-file rcfile))))
  803.  
  804.  
  805. (defun gopher-add-bookmark (arg)
  806.   "Add current object to menu of bookmarks.
  807. With numeric prefix argument N, add Nth object."
  808.   (interactive "P")
  809.   (if (gopher-bookmark-directory-p)
  810.       (error "That item is already a bookmark!")
  811.     (let ((existing-bookmarks gopher-bookmarks)
  812.           (new-bookmarks (make-vector (1+ (length gopher-bookmarks)) nil))
  813.           (obj (copy-sequence (gopher-directory-obj arg)))
  814.           (l (length gopher-bookmarks)))
  815.       (gopher-set-object-descr
  816.        obj
  817.        (read-from-minibuffer "Node Name: "
  818.                              (gopher-object-descr obj)))
  819.       (aset new-bookmarks l obj)
  820.       (while (> l 0)
  821.         (progn (setq l (1- l))
  822.                (aset new-bookmarks l (aref existing-bookmarks l))))
  823.       (setq gopher-bookmarks new-bookmarks
  824.             gopher-bookmarks-modified t
  825.         gopher-bookmark-modified-tick (1+ gopher-bookmark-modified-tick))
  826.       )))
  827.  
  828.  
  829. (defun gopher-delete-bookmark (arg)
  830.   "Delete current bookmark.
  831. With numeric prefix argument N, delete Nth bookmark."
  832.   (interactive "P")
  833.   (if (not (gopher-bookmark-directory-p))
  834.       (error "Can only delete object in Bookmark directory.")
  835.     (let ((new-bookmarks (make-vector (1- (length gopher-bookmarks)) nil))
  836.           (pos (1- (gopher-directory-n arg)))
  837.           (l (length gopher-bookmarks))
  838.           (i 0))
  839.       (while (< i pos)
  840.         (progn (aset new-bookmarks i (aref gopher-bookmarks i))
  841.                (setq i (1+ i))))
  842.       (while (< i (1- l))
  843.         (progn (aset new-bookmarks i (aref gopher-bookmarks (1+ i)))
  844.                (setq i (1+ i))))
  845.       (setq gopher-killed-bookmark (aref gopher-bookmarks pos)
  846.         gopher-bookmarks new-bookmarks
  847.             gopher-dir new-bookmarks
  848.             gopher-bookmarks-modified t
  849.         gopher-bookmark-modified-tick (1+ gopher-bookmark-modified-tick))
  850.       (let ((ppos (1- (gopher-directory-n nil))))
  851.     (if (< pos ppos)
  852.         (setq ppos (1- ppos)))
  853.     (gopher-format-bookmarks)
  854.     (goto-char (point-min))
  855.     (forward-line ppos)
  856.     (forward-char 7)))
  857.     (if (= (point) (point-max)) (previous-line 1))
  858. ;    (let ((buffer-read-only nil))
  859. ;      (beginning-of-line 1)
  860. ;      (kill-line 1)
  861. ;      (if (= (point) (point-max)) (previous-line 1)))
  862.     (if (zerop (length gopher-bookmarks))
  863.         (gopher-last-node))))
  864.  
  865.  
  866. (defun gopher-yank-bookmark (arg)
  867.   "Yank the most recently killed bookmark at the current position.
  868. With numeric prefix argument N, yank into position N."
  869.   (interactive "P")
  870.   (cond ((not (gopher-bookmark-directory-p))
  871.      (error "Can only yank bookmark objects into bookmark directory."))
  872.     ((null gopher-killed-bookmark)
  873.      (error "No killed bookmark object"))
  874.     (t
  875.      (let* ((len (length gopher-bookmarks))
  876.         (new-bookmarks (make-vector (1+ len) nil))
  877.         (pos (1- (gopher-directory-n arg)))
  878.         i)
  879.  
  880.        (if (or (< pos 0) (> pos (length gopher-bookmarks)))
  881.            (error "Out of range."))
  882.  
  883.        (setq i (1- pos))
  884.        (while (>= i 0)
  885.          (aset new-bookmarks i (aref gopher-bookmarks i))
  886.          (setq i (1- i)))
  887.  
  888.        (aset new-bookmarks pos gopher-killed-bookmark)
  889.  
  890.        (setq i pos)
  891.        (while (< i len)
  892.          (aset new-bookmarks (1+ i) (aref gopher-bookmarks i))
  893.          (setq i (1+ i)))
  894.  
  895.        (setq gopher-bookmarks new-bookmarks
  896.          gopher-bookmarks-modified t
  897.          gopher-killed-bookmark nil
  898.          gopher-bookmark-modified-tick
  899.            (1+ gopher-bookmark-modified-tick))
  900.  
  901.        (let ((ppos (1- (gopher-directory-n nil))))
  902.          (if (<= pos ppos)
  903.          (setq ppos (1+ ppos)))
  904.          (gopher-format-bookmarks)
  905.          (goto-char (point-min))
  906.          (forward-line ppos)
  907.          (forward-char 7))
  908.        ))))
  909.        
  910.  
  911. (defun gopher-bookmark-directory-p ()
  912.   "Return T if currently displaying Bookmark directory."
  913.   gopher-bookmark-directory-p)
  914. ;  (equal gopher-dir gopher-bookmarks))
  915.  
  916.  
  917. (defun gopher-read-lisp-bookmarks (fn)
  918.   "currently unsupported"
  919.   (error "gopher-read-lisp-bookmark is not yet supported.  Sorry."))
  920.  
  921. (defun gopher-save-lisp-bookmarks (fn)
  922.   "currently unsupported"
  923.   (error "gopher-save-lisp-bookmark is not yet supported.  Sorry."))
  924.  
  925.  
  926.  
  927. ;;;;--------------------------------------------------------------------------
  928. ;;;; gopher documents
  929. ;;;;
  930.  
  931.  
  932. (defun gopher-document-object (obj oldbuf &optional end-regexp)
  933.   "Retrieve and display a gopher document.
  934. Optional argument END-REGEXP is used if the data will not be ended by `.'."
  935.  
  936.   (let ((docbuf (gopher-get-doc-buf (gopher-object-descr obj))))
  937.  
  938.     ;; Snarf the data into the buffer.
  939.     (gopher-retrieve-document-cleanly (docbuf
  940.                                        (gopher-object-selector obj)
  941.                                        (gopher-object-host     obj)
  942.                                        (gopher-object-port     obj)
  943.                                        end-regexp)
  944.  
  945.         (progn
  946.           (kill-buffer docbuf)
  947.           (error "Problems retrieving document."))
  948.       
  949.       ;; Turn on document mode and put the description in the mode line.
  950.       (switch-to-buffer docbuf)
  951.       (gopher-make-local-vars
  952.        'gopher-last oldbuf)
  953.       (goto-char (point-min))
  954.       (gopher-document-mode)
  955.       (setq mode-line-buffer-identification (concat "Gopher: "
  956.                                                     (gopher-object-descr obj)))
  957.       )))
  958.  
  959.  
  960. ;; keymap for document mode
  961. (suppress-keymap gopher-document-mode-map)
  962.  
  963. ;Virginia Peck <vapeck@cs>  Mon Aug 10 21:44:35 1992
  964. ;;(define-key gopher-document-mode-map "\C-xk"  'gopher-last-node)
  965.  
  966. (define-key gopher-document-mode-map "l"  'gopher-last-node)
  967. (define-key gopher-document-mode-map "q"  'gopher-last-node)
  968. (define-key gopher-document-mode-map "u"  'gopher-last-node)
  969. (define-key gopher-document-mode-map " "  'scroll-up)
  970. (define-key gopher-document-mode-map "\C-?"  'scroll-down)
  971. (define-key gopher-document-mode-map "\r"  'gopher-scroll-one-line-up)
  972.  
  973.  
  974. (defun gopher-document-mode nil
  975.   "Gopher document mode.
  976.  
  977. \\{gopher-document-mode-map}
  978. "
  979.   (use-local-map gopher-document-mode-map)
  980.   (setq major-mode 'gopher-document-mode)
  981.   (setq mode-name "gopher doc")
  982.   (run-hooks 'gopher-document-mode-hook)
  983.   (setq buffer-read-only t))
  984.  
  985.  
  986. ;; from gosmacs.el
  987. (defun gopher-scroll-one-line-up (&optional arg)
  988.   "Scroll the selected window up (forward in the text) one line (or N lines)."
  989.   (interactive "p")
  990.   (scroll-up (or arg 1)))
  991.  
  992.  
  993. ;;;;--------------------------------------------------------------------------
  994. ;;;; CSO handling.
  995. ;;;;
  996. ;;;; uses a subset of forms mode to handle data entry.
  997. ;;;;
  998.  
  999. (defun gopher-cso-object (obj oldbuf)
  1000.   "Display a CSO lookup form."
  1001.  
  1002.   ;; The following will create a buffer displaying the form described
  1003.   ;; by the list in the last argument (cf. forms-mode).  When the user
  1004.   ;; accepts the data in the form (by pressing `C-c RET'), the function
  1005.   ;; gopher-do-cso will be called with the data the user supplied.
  1006.   (gopher-form (gopher-object-descr obj)
  1007.                'gopher-do-cso
  1008.                4
  1009.                '("====== phone directory lookup ======"
  1010.                  "\n Press `C-c RET' to lookup, `C-c l' to return to the last gopher object."
  1011.                  "\n (you must fill in at least one of the first three fields)"
  1012.                  "\n"
  1013.                  "Name    : "   1
  1014.                  "\n"
  1015.                  "Phone   : "   2
  1016.                  "\n"
  1017.                  "E-Mail  : "   3
  1018.                  "\n"
  1019.                  "Address : "   4
  1020.                  ))
  1021.  
  1022.   ;; Record gopher-last so we gopher-last-node knows where to go.
  1023.   ;; Record gopher-obj so gopher-do-cso knows what server to query.
  1024.   (gopher-make-local-vars
  1025.    'gopher-last oldbuf
  1026.    'gopher-obj  obj))
  1027.  
  1028.  
  1029. (defconst gopher-cso-fields '("name" "phone" "email" "address")
  1030.   "Field names to use in CSO queries.")
  1031.  
  1032. (defun gopher-do-cso (vals)
  1033.   "Make a CSO query.  VALS is the data the user entered in the form,
  1034. as a list of strings."
  1035.  
  1036.   ;; Check that the required data was provided.
  1037.   (if (zerop (+ (length (nth 0 vals))
  1038.                 (length (nth 1 vals))
  1039.                 (length (nth 2 vals))))
  1040.       (error "Must specify name, phone, or email."))
  1041.  
  1042.   (let ((query "query")
  1043.         (fields gopher-cso-fields)
  1044.         (obj gopher-obj))
  1045.  
  1046.     ;; Form the query string
  1047.     (while vals
  1048.  
  1049.       (if (not (zerop (length (car vals))))
  1050.           (setq query (concat query " " (car fields) "=" (car vals))))
  1051.  
  1052.       (setq vals (cdr vals))
  1053.       (setq fields (cdr fields)))
  1054.  
  1055.     ;; Use this string as the object selector.
  1056.     (gopher-set-object-selector gopher-obj query)
  1057.  
  1058.     ;; Retrieve the data from the server.  Unlike gopher, the CSO data
  1059.     ;; does not use `.' as a terminator.
  1060.     (gopher-document-object gopher-obj (current-buffer) "^[2-9]")
  1061.  
  1062.     ;; Strip CSO control information from the buffer.
  1063.     (gopher-clean-cso-buffer obj)))
  1064.  
  1065.  
  1066. (defun gopher-clean-cso-buffer (obj)
  1067.   "Strip CSO control information from the current buffer."
  1068.  
  1069.   (let ((req "")
  1070.         (buffer-read-only nil)
  1071.         beg nreq)
  1072.     (goto-char (point-min))
  1073.     (insert "\n")
  1074.     (while (not (eobp))
  1075.       (cond ((and (>= (following-char) ?3) (<= (following-char) ?9))
  1076.              (delete-char 4)
  1077.              (insert (concat (gopher-object-selector obj) "\n")))
  1078.             
  1079.             ((eq (following-char) ?-)
  1080.              (delete-char 5)
  1081.              (setq beg (point))
  1082.              (skip-chars-forward "^:")
  1083.              (setq nreq (buffer-substring beg (point)))
  1084.              (goto-char beg)
  1085.              (or (string= req nreq)
  1086.                  (insert (concat "--------------------------"
  1087.                                  "-----------------------------\n")))
  1088.              (setq req nreq)
  1089.              (setq beg (point))
  1090.              (skip-chars-forward "^:")
  1091.              (forward-char)
  1092.              (delete-region beg (point)))
  1093.  
  1094.             (t
  1095.              (setq beg (point))
  1096.              (forward-line 1)
  1097.              (delete-region beg (point))
  1098.              (forward-line -1))
  1099.             )
  1100.       (forward-line 1))
  1101.  
  1102.     (goto-char (point-min))
  1103.     (delete-char 1)))
  1104.  
  1105.  
  1106. ;;;;--------------------------------------------------------------------------
  1107. ;;;; indices.
  1108. ;;;;
  1109. ;;;; To query an index, the search string is appended to the selector.
  1110. ;;;; The index returns a gopher directory.
  1111. ;;;;
  1112.  
  1113.  
  1114. (defun gopher-index-object (obj oldbuf)
  1115.   "Query a gopher directory object."
  1116.  
  1117.   ;; Get the search string from the user.
  1118.   (let ((str (read-from-minibuffer "Key: "))
  1119.         (newobj (copy-sequence obj)))
  1120.  
  1121.     ;; Append it to the selector and retrieve the modified object
  1122.     ;; like a directory.
  1123.     (setq str (gopher-trim-blanks str))
  1124.     (if (> (length str) 0)
  1125.         (progn
  1126.           (gopher-set-object-selector newobj
  1127.                                       (concat (gopher-object-selector obj) "\t"
  1128.                                               str))
  1129.           (gopher-directory-object newobj (current-buffer)))
  1130.       )))
  1131.  
  1132.  
  1133.  
  1134. ;;;;--------------------------------------------------------------------------
  1135. ;;;; telneting.
  1136. ;;;;
  1137.  
  1138. (defun gopher-telnet-object (obj oldbuf)
  1139.   "Start a telnet session to a gopher object.
  1140. If gopher-telnet-command is nonnil, then that is a command to start
  1141. a telnet session in a subprocess.  Otherwise, the emacs-lisp telnet
  1142. package is used."
  1143.   
  1144.   ;; make the telnet argument string
  1145.   (let ((arg (gopher-object-host obj))
  1146.         (port (gopher-object-port obj)))
  1147.     (if (not (zerop port))
  1148.         (setq arg (concat arg 
  1149.                           (if (eq system-type 'vax-vms)
  1150.                               "/port="
  1151.                             " ")
  1152.                           port)))
  1153.  
  1154.     (if gopher-telnet-command
  1155.  
  1156.         ;; start up telnet as a separate process
  1157.         (save-window-excursion
  1158.           (gopher-background
  1159.            (concat gopher-telnet-command " " arg)))
  1160.  
  1161.       ;; use telnet-mode
  1162.       (telnet arg)
  1163.       ;; set things up so we can get back to the last node.
  1164.       (gopher-make-local-vars
  1165.        'gopher-last oldbuf
  1166.        'gopher-telnet-process-name (concat arg "-telnet"))
  1167.       (local-set-key "\C-cl" 'gopher-telnet-quit)
  1168.       (local-set-key "\C-xk" 'gopher-telnet-quit)
  1169.       )
  1170.  
  1171.     ;; show the login info to the user
  1172.     (if (not (zerop (length (gopher-object-selector obj))))
  1173.         (progn
  1174.           (beep)
  1175.           (message (concat 
  1176.                     "Login as: "
  1177.                     (gopher-object-selector obj)
  1178.                     ))
  1179.           ))
  1180.     ))
  1181.  
  1182.  
  1183. (defun gopher-telnet-quit nil
  1184.   "Clean up a telnet session and return to the previous gopher node."
  1185.   (interactive)
  1186.   (condition-case nil
  1187.       (delete-process gopher-telnet-process-name)
  1188.     (error t))
  1189.   (gopher-last-node))
  1190.  
  1191.  
  1192.  
  1193. ;;;;--------------------------------------------------------------------------
  1194. ;;;; Various opaque objects.  Just save them in a file for now.
  1195. ;;;;
  1196.  
  1197. (defun gopher-binary-object (obj oldbuf)
  1198.   "Retrieve a gopher object and save it to a file,
  1199. without trying to interpret it in any way."
  1200.   (let ((fname (read-file-name "File to save in: "))
  1201.         (buf (gopher-get-doc-buf (gopher-object-descr obj))))
  1202.  
  1203.     (gopher-retrieve-document-cleanly (buf
  1204.                                        (gopher-object-selector obj)
  1205.                                        (gopher-object-host     obj)
  1206.                                        (gopher-object-port     obj)
  1207.                                        'none)
  1208.  
  1209.         (progn
  1210.           (error "Problems retrieving object.")
  1211.           (kill-buffer buf))
  1212.  
  1213.       (save-excursion
  1214.         (set-buffer buf)
  1215.         (write-file fname))
  1216.       (kill-buffer buf)
  1217.       )))
  1218.  
  1219.  
  1220. ;;;;--------------------------------------------------------------------------
  1221. ;;;; forms stuff
  1222. ;;;;
  1223. ;;;; Uses some of the internal routines from forms.el to present
  1224. ;;;; a form which is not associated with a file.
  1225. ;;;;
  1226.  
  1227. (defun gopher-form (form-name accept-action number-of-fields format-list)
  1228.   "Display a buffer containing a form for the user to enter data.
  1229. The form is described by NUMBER-OF-FIELDS and FORMAT-LIST (cf. forms-mode).
  1230. FORM-NAME is a string to put in the modeline.
  1231. When the user accepts the data in the form by pressing `C-c RET', the
  1232. function ACCEPT-ACTION is called with a list of the strings which
  1233. the user entered."
  1234.  
  1235.   (switch-to-buffer (generate-new-buffer "*gopher form*"))
  1236.  
  1237.   (gopher-make-local-vars
  1238.    'forms-format-list   format-list
  1239.    'forms-number-of-fields number-of-fields
  1240.    'forms-field-sep        "\t"
  1241.    'forms-read-only        nil
  1242.    'forms-multi-line       nil
  1243.    'forms--number-of-markers nil
  1244.    'forms--markers           nil
  1245.    'forms--format            nil
  1246.    'forms--parser            nil
  1247.    'forms--dynamic-text      nil
  1248.    'forms-fields             nil
  1249.    'forms-the-record-list    nil
  1250.    'forms-accept-action      accept-action
  1251.    )
  1252.  
  1253.   (forms--process-format-list)
  1254.   (forms--make-format)
  1255.   (forms--make-parser)
  1256.  
  1257.   (erase-buffer)
  1258.  
  1259.   ;; make local variables
  1260.   (make-local-variable 'forms--file-buffer)
  1261.   (make-local-variable 'forms--total-records)
  1262.   (make-local-variable 'forms--current-record)
  1263.   (make-local-variable 'forms--the-record-list)
  1264.   (make-local-variable 'forms--search-rexexp)
  1265.  
  1266.   ;; set the major mode indicator
  1267.   (setq major-mode 'gopher-form-mode)
  1268.   (setq mode-name "gopher form")
  1269.  
  1270.   (set-buffer-modified-p nil)
  1271.  
  1272.   (use-local-map gopher-form-mode-map)
  1273.  
  1274.   (forms--show-record (make-string (1- number-of-fields) ?\t))
  1275.  
  1276.   (run-hooks 'gopher-form-mode-hooks))
  1277.  
  1278.  
  1279. (defun gopher-form-accept nil
  1280.   (interactive)
  1281.  
  1282.   (funcall forms-accept-action (forms--parse-form)))
  1283.  
  1284. (define-key gopher-form-mode-map "\C-c\r"  'gopher-form-accept)
  1285. (define-key gopher-form-mode-map "\C-cl"   'gopher-last-node)
  1286.  
  1287.  
  1288. ;;;;--------------------------------------------------------------------------
  1289. ;;;; low-level communications routines
  1290. ;;;;
  1291.  
  1292. (defun gopher-retrieve-document (buf sel host port &optional end-regexp)
  1293.   "Retrieve a gopher object into BUF.
  1294. The object is identified by a SEL HOST PORT triple.
  1295. Optional argument END-REGEXP is used for data which is not `.'-terminated.
  1296. If END-REGEXP is non-nil and not a string, then it is assumed that
  1297. the data is binary, and reading will continue until the sender disconnects.
  1298. Returns NIL if an error occured during the attempt to retrieve the
  1299. document, otherwise T.
  1300. "
  1301.  
  1302.  
  1303.   ;; Default is single period termination.
  1304.   (or end-regexp (setq end-regexp "^\\.\r$"))
  1305.  
  1306.   (save-excursion
  1307.     (set-buffer buf)
  1308.     (erase-buffer)
  1309.  
  1310.     (let ((h (assoc host gopher-hostname-aliases)))
  1311.       (if h (setq host (cdr h))))
  1312.  
  1313.     ;; open the connection to the server
  1314.     (let (wait
  1315.       (gopher-server-process
  1316.            (condition-case nil
  1317.                (open-network-stream "gopher" (current-buffer)
  1318.                                     host port)
  1319.              (error nil))))
  1320.  
  1321.       (cond (gopher-server-process
  1322.              
  1323.              ;; keep the emacs end-of-process status line out of the buffer
  1324.              (set-process-sentinel gopher-server-process 'gopher-sentinel)
  1325.  
  1326.              ;; send the selector to the server
  1327.              (process-send-string gopher-server-process (concat sel "\r\n"))
  1328.  
  1329.              ;; receive the response from the server
  1330.              ;; based on nntp.el from GNUS
  1331.              (setq wait t)
  1332.              (while wait
  1333.                (if (stringp end-regexp)
  1334.                    (progn
  1335.                      (goto-char (point-max))
  1336.                      (forward-line -1)))
  1337.                (if (and (stringp end-regexp)
  1338.                         (looking-at end-regexp))
  1339.                    (setq wait nil)
  1340.                  (if (not (memq (process-status gopher-server-process) '(open run)))
  1341.                      (progn
  1342.                        (ding)
  1343.                        (message "gopher: connection closed")
  1344.                        (setq wait nil))
  1345.                    (if gopher-debug-read
  1346.                        (message "gopher: Reading..."))
  1347.                    (cond (gopher-buggy-accept
  1348.                           (sit-for 1))
  1349.                          ((and (boundp 'epoch::version) epoch::version)
  1350.                           (accept-process-output gopher-server-process 2))
  1351.                          (t
  1352.                           (accept-process-output gopher-server-process))
  1353.                          )
  1354.                    (if gopher-debug-read
  1355.                        (message " ")))
  1356.                  ))
  1357.  
  1358.              ;; be sure the net connection has gone away...
  1359.              (condition-case nil
  1360.                  (delete-process gopher-server-process)
  1361.                (error t))
  1362.  
  1363.              ;; clean up the text buffer
  1364.              (if (stringp end-regexp)
  1365.                  (gopher-clean-text))
  1366.  
  1367.              t)
  1368.  
  1369.             (t nil))
  1370.       )))
  1371.  
  1372.  
  1373. ;;; adapted from GNUS
  1374. (defun gopher-clean-text ()
  1375.   "Decode text transmitted by gopher.
  1376. 0. Delete status line.
  1377. 1. Delete `^M' at end of line.
  1378. 2. Delete `.' at end of buffer (end of text mark).
  1379. 3. Delete `.' at beginning of line.   (does gopher want this?)"
  1380.  
  1381.   ;; Insert newline at end of buffer.
  1382.   (goto-char (point-max))
  1383.   (if (not (bolp))
  1384.       (insert "\n"))
  1385.   ;; Delete `^M' at end of line.
  1386.   (goto-char (point-min))
  1387.   (replace-regexp "\r[^\n]*$" "")
  1388. ;  (goto-char (point-min))
  1389. ;  (while (not (eobp))
  1390. ;    (end-of-line)
  1391. ;    (if (= (preceding-char) ?\r)
  1392. ;       (delete-char -1))
  1393. ;    (forward-line 1)
  1394. ;    )
  1395.   ;; Delete `.' at end of buffer (end of text mark).
  1396.   (goto-char (point-max))
  1397.   (forward-line -1)                     ;(beginning-of-line)
  1398.   (while (looking-at "^\\.$")
  1399.     (delete-region (point) (progn (forward-line 1) (point)))
  1400.     (forward-line -1))
  1401.   ;; Replace `..' at beginning of line with `.'.
  1402.   (goto-char (point-min))
  1403.   ;; (replace-regexp "^\\.\\." ".")
  1404.   (while (search-forward "\n.." nil t)
  1405.     (delete-char -1))
  1406.   )
  1407.  
  1408.  
  1409. (defun gopher-sentinel (proc status)
  1410.   nil)
  1411.  
  1412. ;;; gopher.el ends here
  1413.  
  1414. ;;;(gopher.el) Local Variables:
  1415. ;;;(gopher.el) eval: (put 'gopher-retrieve-document-cleanly 'lisp-indent-hook 2)
  1416. ;;;(gopher.el) End:
  1417.  
  1418.